FROM centos:centos7.9.2009 as base
RUN yum makecache && yum install -y centos-release-scl
RUN yum makecache && yum install -y yum-utilsnet-tools vim 
RUN yum install -y devtoolset-7-gcc devtoolset-7-gcc-c++ git pcre-devel zlib-devel openssl-devel  luajit-devel make pcre-devel zlib-devel 
RUN ln -s  -f /opt/rh/devtoolset-7/root/bin/* /usr/bin/
RUN yum install -y wget && wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

FROM base as ngx_builder

WORKDIR /build


ENV RESTY_VERSION="1.21.4.1"

RUN wget https://openresty.org/download/openresty-${RESTY_VERSION}.tar.gz
RUN tar -xf openresty-${RESTY_VERSION}.tar.gz

ARG RESTY_LUAJIT_OPTIONS="--with-luajit-xcflags='-DLUAJIT_NUMMODE=2'"

ARG RESTY_CONFIG_OPTIONS="\
    --with-compat \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_v2_module \
    --without-mail_smtp_module --without-mail_imap_module --without-mail_pop3_module \
    --with-stream \
    --with-stream_ssl_module \
    --with-threads \
    --modules-path=/etc/nginx/modules/ \
    --error-log-path=/var/nginx/logs/error.log \
    --http-log-path=/var/nginx/logs/access.log  \
    --conf-path=/etc/nginx/nginx.conf \
    "
RUN mkdir -p /etc/nginx/modules/
RUN cd /build/openresty-${RESTY_VERSION} && ./configure --prefix=/usr/local/openresty  ${RESTY_CONFIG_OPTIONS} -j8 ${RESTY_LUAJIT_OPTIONS}
RUN cd /build/openresty-${RESTY_VERSION} && make install -j8

# https://github.com/opentracing-contrib/nginx-opentracing/releases/download/v0.26.0/linux-amd64-nginx-1.21.4-ngx_http_module.so.tgz
COPY linux-amd64-nginx-1.21.4-ngx_http_module.so.tgz .
COPY nginx.conf /etc/nginx/nginx.conf
RUN  tar -xf linux-amd64-nginx-1.21.4-ngx_http_module.so.tgz -C /etc/nginx/modules/

#https://github.com/jaegertracing/jaeger-client-cpp/releases v0.4.2
COPY libjaegertracing_plugin.linux_amd64.so /etc/nginx/modules/
COPY etc/jaeger-config.json /etc/jaeger-config.json




FROM centos:centos7.9.2009 
WORKDIR /usr/local/openresty
COPY --from=ngx_builder /etc/nginx /etc/nginx
COPY --from=ngx_builder /usr/local/openresty /usr/local/openresty
COPY --from=ngx_builder /etc/jaeger-config.json /etc/jaeger-config.json
RUN ln -s /etc/nginx/modules /usr/local/openresty/nginx/modules
ADD vhost /etc/nginx/vhost
VOLUME [ "/var/nginx/logs" ,"/etc/nginx/vhost", "/etc/nginx/modules"]
CMD ["/usr/local/openresty/nginx/sbin/nginx", "-g", "daemon off;"]